//
//  HotCollectionViewCell.m
//  SugarPile
//
//  Created by Charlie. on 16/1/18.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "HotCollectionViewCell.h"
#import "LCPrefixHeader.pch"

@interface HotCollectionViewCell ()

@end
@implementation HotCollectionViewCell

-(void)dealloc
{
    [_imgview release];
    [_writingLabel release];
    [_userView release];
    [_dierView release];
    [_pingView release];
    [_plLabel release];
    [_zView release];
    [_zLaber release];
    [_scView release];
    [_moneyLabel release];
    [_senderView release];
    [_albumNameLabel release];
    [_senderNameLabel release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 内容背景
        self.dierView = [[UIView alloc] init];
        [self.contentView addSubview:_dierView];
//        _dierView.backgroundColor = [UIColor redColor];
        [self.dierView release];
        // 主图片
        self.imgview = [[UIImageView alloc] init];
        [self.contentView addSubview:_imgview];
        [_imgview release];
        
        // 自适应的文字
        self.writingLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_writingLabel];
//                _writingLabel.backgroundColor = [UIColor yellowColor];
        _writingLabel.numberOfLines = 0;
        _writingLabel.font = [UIFont systemFontOfSize:9];
        _writingLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
        [self.writingLabel release];
        // 评论 图标
        self.pingView = [[UIImageView alloc] init];
        [self.contentView addSubview:_pingView];
//        _pingView.backgroundColor = [UIColor yellowColor];
        [self.pingView release];
        // 评论文字
        self.plLabel = [[UILabel alloc] init];
        _pingView.image = [UIImage imageNamed:@"pinglun"];
        [self.contentView addSubview:_plLabel];
//        _plLabel.backgroundColor = [UIColor yellowColor];
        _plLabel.font = [UIFont systemFontOfSize:7];
        _plLabel.textColor = [UIColor lightGrayColor];
        _plLabel.textAlignment = NSTextAlignmentCenter;
        [self.plLabel release];
        // 点赞 文字
        self.zLaber = [[UILabel alloc] init];
        [self.contentView addSubview:_zLaber];
//        _zLaber.backgroundColor = [UIColor yellowColor];
        _zLaber.font = [UIFont systemFontOfSize:7];
        _zLaber.textColor = [UIColor lightGrayColor];
        _zLaber.textAlignment = NSTextAlignmentCenter; // 居中
        [self.zLaber release];
        // 点赞 图标
        self.zView = [[UIImageView alloc] init];
        _zView.image = [UIImage imageNamed:@"zan"];
        [self.contentView addSubview:_zView];
//        _zView.backgroundColor = [UIColor yellowColor];
        [self.zView release];
        // 收藏 文字
        self.scLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_scLabel];
//        _scLabel.backgroundColor = [UIColor yellowColor];
        _scLabel.font = [UIFont systemFontOfSize:7];
        _scLabel.textColor = [UIColor lightGrayColor];
        _scLabel.textAlignment = NSTextAlignmentCenter;
        [self.scLabel release];
        // 收藏 图标
        self.scView = [[UIImageView alloc] init];
        _scView.image = [UIImage imageNamed:@"shoucang"];
        [self.contentView addSubview:_scView];
//        _scView.backgroundColor = [UIColor yellowColor];
        [self.scView release];
        
        // 宽度为0.3分割线
        self.userView = [[UIView alloc] init];
        [self.contentView addSubview:_userView];
        _userView.backgroundColor = [UIColor lightGrayColor];
        [self.userView release];
        
        
        // 用户头像图片
        self.senderView = [[UIImageView alloc] init];
        [self.userView addSubview:_senderView];
//        _senderView.backgroundColor = [UIColor blueColor];
        [self.senderView release];
        
        // 用户名
        self.senderNameLabel = [[UILabel alloc] init];
        [self.userView addSubview:_senderNameLabel];
        _senderNameLabel.font = [UIFont systemFontOfSize:11];
//        _senderNameLabel.backgroundColor = [UIColor yellowColor];
        [self.senderNameLabel release];
        
        // 专辑名
        self.albumNameLabel = [[UILabel alloc] init];
        [self.userView addSubview:_albumNameLabel];
        _albumNameLabel.font = [UIFont systemFontOfSize:10];
        _albumNameLabel.textColor = [UIColor lightGrayColor];
//        _albumNameLabel.backgroundColor = [UIColor blueColor];
        [self.albumNameLabel release];
        
        // 商品价钱
        self.moneyLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_moneyLabel];
        //        _MoneyLabel.backgroundColor = [UIColor lightGrayColor];
        _moneyLabel.font = [UIFont systemFontOfSize:11];
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        [self.moneyLabel release];
        
        // 购物车
        self.carView = [[UIImageView alloc] init];
        [self.contentView addSubview:_carView];
        //        _carView.backgroundColor = [UIColor yellowColor];
        [self.carView release];
        
        self.carButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.carView addSubview:self.carButton];
    }
    return self;
}
#pragma mark - 重写set方法
- (void)setModel:(HotModel *)model
{
    // 重写 set 方法
    if (_model != model) {
        [_model release];
        _model = [model retain];
        
        // 重新计算图片高度
        [_imgview sd_setImageWithURL:[NSURL URLWithString:[model.path stringByReplacingOccurrencesOfString:@"_webp" withString:@""]]];
        _writingLabel.text = model.msg;
        [_writingLabel sizeToFit];
        // 设置文本框最大范围
        CGFloat h = [[self class] heightWithText:self.model.msg Size:9];
        if (h > 15.5 * 4) {
            h = 15.5 * 4;
        }
        // 设置图片 frame
//       片的高度 / 图片的宽度
        _imgview.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.width * model.height.doubleValue / model.width.doubleValue);
        // 设置文本 frame
//        // 高度为 h 为通过文本自适应高度 计算得出 文本框左右都留有 5 的边界
        _writingLabel.frame = CGRectMake(5, _imgview.frame.origin.y + _imgview.frame.size.height + 5, self.contentView.frame.size.width - 10, h);
        // 设置 背景大小 图高 + 字高 + 其他空间高80
        _dierView.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, _imgview.bounds.size.height + _writingLabel.bounds.size.height + 75);
        
        // 数据传入 评论Label
        _plLabel.text = model.reply_count.stringValue;
        //设置 评论图片的 frame 大小
        _pingView.frame = CGRectMake(5, _imgview.bounds.size.height + _writingLabel.bounds.size.height + 10, 10, 10);
        // 计算 评论文本宽度
        CGFloat plW = [[self class] widthWithText:self.model.reply_count.stringValue Size:7];
        // 设置 评论数的 frame 尺寸
        //width = 计算出来的 文本宽度
        //height = 10;
        _plLabel.frame = CGRectMake(5 + 10 + 2, _imgview.bounds.size.height + _writingLabel.bounds.size.height + 10, plW, 10);
        
        // 数据传入 赞Label
        _zLaber.text = model.like_count.stringValue;
        //设置 赞的frame
        _zView.frame = CGRectMake(5 + 10 + plW + 2 + 8, _imgview.bounds.size.height + _writingLabel.bounds.size.height + 10, 10, 10);
        // 赞Label 的宽度
        CGFloat zW = [[self class] widthWithText:self.model.like_count.stringValue Size:7];
        // 赞Label 的坐标
        _zLaber.frame = CGRectMake(5 + 10 + plW + 2 + 8 + 10 + 2 , _imgview.bounds.size.height + _writingLabel.bounds.size.height + 10, zW, 10);
        
        //设置收藏图片, 收藏的个数
        _scLabel.text = model.favorite_count.stringValue;
        _scView.frame = CGRectMake(5 + 10 + plW + 2 + 8 + 2 + 8 + zW + 8 , _imgview.bounds.size.height + _writingLabel.bounds.size.height + 10, 10, 10);
        // 收藏Label 的宽度
        CGFloat scW = [[self class] widthWithText:self.model.favorite_count.stringValue Size:7];
        _scLabel.frame = CGRectMake(5 + 10 + plW + 2 + 8 + 2 + 8 + 8 + zW + 2 + 10, _imgview.bounds.size.height + _writingLabel.bounds.size.height + 10, scW, 10);
        // 分割线
        _userView.frame = CGRectMake(0, _imgview.bounds.size.height + _writingLabel.bounds.size.height + 10 + 10 + 10, _imgview.bounds.size.width, 0.3);
        //头像
        _senderView.frame = CGRectMake(5, _userView.bounds.origin.y + 10, 25, 25);
        [_senderView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
        _senderView.layer.cornerRadius = _senderView.bounds.size.width / 2;
        _senderView.clipsToBounds = YES;
        
        //文本的名字
        _albumNameLabel.frame = CGRectMake(5 + _senderView.bounds.size.width + 5, _userView.bounds.origin.y + 10, self.contentView.bounds.size.width - _senderView.bounds.size.width - 10, 10);
        _albumNameLabel.text = model.name;
        
        //用户名
        _senderNameLabel.frame = CGRectMake(5 + _senderView.bounds.size.width + 5 , _albumNameLabel.bounds.origin.y + 10 + 5 + 5, self.contentView.bounds.size.width - _senderView.bounds.size.width - 10, 15);
        _senderNameLabel.text = [NSString stringWithFormat:@"by %@", model.username];
        
        //    // 判断 数据 如果有则 在cell上赋值
        // 判断 淘宝网址如果为空 则 不进行赋值
        if (![model.icon_url isEqualToString:@""]) {
            _moneyLabel.text = [@"￥" stringByAppendingString:[NSString stringWithFormat:@"%.1f", [model.price floatValue]]];
            _moneyLabel.frame = CGRectMake(0,0,_imgview.bounds.size.width / 3, 20);
            _moneyLabel.backgroundColor = [UIColor lightGrayColor];
        }
        [_carView sd_setImageWithURL:[NSURL URLWithString:model.icon_url]];
        _carView.frame = CGRectMake(_dierView.bounds.size.width * 4 / 5, _dierView.bounds.size.height - 70, 20, 20);
        _carButton.frame = _carView.bounds;
    }
}
// 计算文本宽度
+ (CGFloat)widthWithText:(NSString *)text Size:(CGFloat)fontSize
{
    //保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    //设置最大 尺寸
    //高度于label一致
    CGSize size = CGSizeMake((WIDTH - 30) / 2 - 10, 10);
    //计算出 出一个 rect
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的宽度
    return rect.size.width;
}

// 计算文本高度
+(CGFloat)heightWithText:(NSString *)text Size:(CGFloat)fontSize
{
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]};
    // 预设尺寸 文本最大不超过这个尺寸
    // 根据设置的宽度计算高度(宽度和label保持一致)
    CGSize size = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30) / 2 - 10, 500);
  
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // 返回得到的高度
    
    return rect.size.height;
}
// 布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
////    _imgview.frame = layoutAttributes.bounds;
//    _imgview.frame = CGRectMake(0, 0, layoutAttributes.bounds.size.width, layoutAttributes.bounds.size.height - 90);
//    _moneyLabel.frame = CGRectMake(layoutAttributes.bounds.size.width * 2 / 3, layoutAttributes.bounds.size.height - 115, layoutAttributes.bounds.size.width / 3, 25);
//    
//    _dierView.frame = CGRectMake(0, layoutAttributes.bounds.size.height - 90, layoutAttributes.bounds.size.width, 50);
//    _writingLabel.frame = CGRectMake(5, 5, layoutAttributes.bounds.size.width - 10, 26);
//    _pingView.frame = CGRectMake(5, 35, 10, 10);
//    _plLabel.frame = CGRectMake(15, 35, 10, 10);
//
//    _zView.frame = CGRectMake(30, 35, 10, 10);
//    _zLaber.frame = CGRectMake(40, 35, 12, 10);
//    
//    _scView.frame = CGRectMake(55, 35, 10, 10);
//    _scLabel.frame = CGRectMake(65, 35, 17, 10);
//    
//    _carView.frame = CGRectMake(135, 33, 18, 15);
//    
//    _userView.frame = CGRectMake(0, layoutAttributes.bounds.size.height - 90 + 50, layoutAttributes.bounds.size.width, 40);
//    
//    _senderView.frame = CGRectMake(5, 5, 30, 30);
//    _senderView.layer.cornerRadius = _senderView.bounds.size.width / 2;
//    // 设的角度切
//    _senderView.clipsToBounds = YES;
//    
//    _senderNameLabel.frame = CGRectMake(45, 5, 100, 15);
//    _albumNameLabel.frame = CGRectMake(45, 20, 100, 15);
}

@end
